package com.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.dto.DishDto;
import com.reggie.entity.Dish;
import com.reggie.entity.DishFlavor;
import com.reggie.mapper.DishMapper;
import com.reggie.service.CategoryService;
import com.reggie.service.DishFlavorService;
import com.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @program: reggie
 * @author:
 * @create: 2022-09-28 09:01
 **/
@Service
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;
    /**
     * 表dish和dishFlavor两个表的保存
     * */
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //将dish中的数据保存
        this.save(dishDto);

        //获取dish中保存数据的id值
        Long id = dishDto.getId();


        //将dishFlavor中的数据保存
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(id);
        }
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 表dish和dishFlavor两个表数据的删除
     *
     * @param ids
     */
    @Override
    public void delDishWithFlavor(Long[] ids) {
        //数据转成集合
        this.removeByIds(Arrays.asList(ids));

        for (Long id : ids) {
            LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DishFlavor::getDishId,id);
            dishFlavorService.remove(wrapper);
        }

    }

    /**
     * 获取dish和dishFlavor中的数据
     * */
    @Override
    public DishDto getDishWithFlavor(Long id) {
        DishDto dishDto = new DishDto();
        Dish dish = this.getById(id);
        //将dish表中封装的数据放在dishDto中
        BeanUtils.copyProperties(dish,dishDto);
        /*Long categoryId = dish.getCategoryId();
        Category category = categoryService.getById(categoryId);
        String categoryName = category.getName();
        dishDto.setCategoryName(categoryName);*/
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());

        dishDto.setFlavors(dishFlavorService.list(wrapper));
        return dishDto;
    }

    /**
     * 菜品数据的修改
     * 修改dish表中数据
     * 删除和重新添加dishFlavors表中的数据
     * */
    @Override
    public void updateDish(DishDto dishDto) {
        List<DishFlavor> flavors = dishDto.getFlavors();
        //更新dish表中的数据
        this.updateById(dishDto);
        //删除dishflavors表中的数据
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(wrapper);
        //重新添加dishflavors表中的数据
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
        }
        dishFlavorService.saveBatch(flavors);
    }

}
